﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Documents;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.DocumentService;
using MobileSales.Website.Localization;


namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class PaymentMethodsController : ControllerBase
    {
        #region Index
        //
        // GET: /PaymentMethods/

        public ActionResult Index()
        {
            IEnumerable<PaymentMethod> meths = null;
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                meths = documentService.GetPaymentMethodList();
                documentService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorPaymentList, MessageType.Error);
            }
            return View(meths);
        } 
        #endregion

        #region Create
        //
        // GET: /PaymentMethods/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /PaymentMethods/Create

        [HttpPost]
        public ActionResult Create(PaymentMethod method)
        {
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                documentService.StorePaymentMethod(method);
                documentService.Close();
                this.SetMessage(InfoStrings.PaymentAdded, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorPaymentAdding, MessageType.Error);

            }
            return View(method);
        } 
        #endregion

        #region Edit
        //
        // GET: /PaymentMethods/Edit/5

        public ActionResult Edit(int id)
        {
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                PaymentMethod meth = documentService.GetPaymentMethod(id);
                documentService.Close();
                return View(meth);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.PaymentNotFound, MessageType.Error);
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /PaymentMethods/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, PaymentMethod method)
        {
            try
            {
                method.Id = id;
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                documentService.StorePaymentMethod(method);
                documentService.Close();
                this.SetMessage(InfoStrings.PaymentUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorPaymentUpdating, MessageType.Error);

            }
            return View(method);
        } 
        #endregion

        #region Delete
        //
        // GET: /PaymentMethods/Delete/5

        public ActionResult Delete(int id)
        {
            return Edit(id);
        }

        //
        // POST: /PaymentMethods/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.RemoveTax(id);
                productService.Close();
                this.SetMessage(InfoStrings.TaxDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaxDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
